package dbmanager;

import java.sql.*;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.Vector;

import utility.ConvertToLower;

public class GetIdName
{
	String tablename;
	DBManager database ;
	ResultSet rs2;
	Cache cache = new Cache();
	static int CASHESIZE = 200;
	
	public GetIdName(DBManager database) 
	{
		tablename = "master_table";
		this.database = database;
	}
	
	public GetIdName(String tablename, DBManager database)
	{
		this.tablename = tablename;
		this.database = database;
	}
	
	static class Cache
	{
		
		static LinkedList<Object> id = new LinkedList<Object>();
		static LinkedList<Object> name = new LinkedList<Object>();
		static LinkedList<Object> use = new LinkedList<Object>();
		
		void setCache(Object id, Object name)
		{
			if(Cache.id.size()<=CASHESIZE)
			{
				if(Cache.id.contains(id)!=true)
				{
					Cache.id.add(id);
					Cache.name.add(name.toString().toLowerCase());
					Cache.use.add("1");
				}
			}
			else
			{
				int index = minUse(use);
				if(Cache.id.contains(id)!=true)
				{
					Cache.id.set(index,id);
					Cache.name.set(index,name.toString().toLowerCase());
					Cache.use.set(index,"1");
				}
			}
		}
		
		int minUse(LinkedList< Object> use)
		{
			int index=0;
			int min = Integer.parseInt(use.get(0).toString());
			for(int i=1;i<use.size();i++)
			{
				int tempmin = Integer.parseInt(use.get(i).toString());
				if(min>tempmin)
				{
					min = tempmin;
					index=i;
				}
			}
			return index;
		}
		
		Object getid(Object name)
		{
			Object id = new Object();
			name = name.toString().toLowerCase();
			if(Cache.name.contains(name))
			{
				try
				{
					int index = Cache.name.indexOf(name);
					id = Cache.id.get(index).toString();
					int use = Integer.parseInt(Cache.use.get(index).toString());
					Cache.use.set(index, (use+1));
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
			else
				id="no$";
			return id;
		}
		
		Object getname(Object id)
		{
			Object name = new Object();
			id = id.toString().toLowerCase();
			if(Cache.id.contains(id))
			{
				try
				{
					int index = Cache.id.indexOf(id);
					name = Cache.name.get(index).toString();
					int use = Integer.parseInt(Cache.use.get(index).toString());
					Cache.use.set(index, (use+1));
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
			else
				name="no$";
			return name;
		}
	}
	
	public Hashtable<Object, Object> ConvertValuetoMasterName(Hashtable<Object, Object>temp)
	{
		Hashtable<Object, Object>table = new Hashtable<Object, Object>();
		Enumeration<Object>tableenumeration = temp.keys();
		while(tableenumeration.hasMoreElements())
		{
			Object key = tableenumeration.nextElement();
			String value = (temp.get(key).toString());
			String keyStr =getItem(value.toString());
			if(keyStr.equals("no$") || value.equals("0"))
				keyStr = value.toString();
			table.put(key, keyStr);
		}
		return table;
	}
	public Hashtable<Object, Object> ConvertValuetoMasterName(Hashtable<Object, Object>temp,String process,String tablename)
	{
		Hashtable<Object, Object>table = new Hashtable<Object, Object>();
		Enumeration<Object>enum1 = temp.keys();
		while(enum1.hasMoreElements())
		{
			String key = enum1.nextElement().toString();
			ResultSet rs =  database.getSelect("select from_master from  command where fieldname='"+key+"' and process='"+process+"' and msgtype='"+tablename+"'");
			
			try
			{
				if(rs.next())
				{
					String from_master = rs.getString("from_master");
					if(from_master.equals("yes"))
					{
						String value1 = getItem(temp.get(key).toString());
						table.put(key, value1);
					}
					else
					{
						String value1 = temp.get(key).toString();
						table.put(key, value1);
						
					}
				}	
				else
				{
					String value1 = temp.get(key).toString();
					table.put(key, value1);
					
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return table;
	}
	public Hashtable<Object, Object> ConvertvalueToMasterId(Hashtable<Object, Object> hashobj,String process,String tablename)
	{
		Enumeration< Object> enum1 = hashobj.keys();
		while(enum1.hasMoreElements())
		{
			String key = enum1.nextElement().toString();
			ResultSet rs =  database.getSelect("select from_master from  command where fieldname='"+key+"' and process='"+process+"' and msgtype='"+tablename+"'");
			
			try
			{
				if(rs.next())
				{
					String from_master = rs.getString("from_master");
					if(from_master.equals("yes"))
					{
						String value1 = getId(hashobj.get(key).toString());
						hashobj.put(key, value1);
					}
					else
					{
						String value1 = hashobj.get(key).toString();
						hashobj.put(key, value1);
						
					}
				}	
				else
				{
					String value1 = hashobj.get(key).toString();
					hashobj.put(key, value1);
					
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return hashobj;
	
		
	}
	public String getItem(String id)
 	{
		//System.out.println("ID IN getItemName "+id);
		String name = cache.getname(id).toString();
		if(name.equals("no$")==true && id.length()!=0)
		{
			try
			{

 				rs2= database.getSelect("Select name from "+tablename+" where id = '"+id+"'");
 				if(rs2.next())
 				{
	 				name = rs2.getString("name");
	 				cache.setCache(id, name);
	 				rs2.close();
	 				return name;
 				}
 				return("no$");	
 			}
  			catch(Exception e)
  			{
  				System.out.println("utility::GetIdName::getItem()::Exception -->>"+e.getMessage());
  				return("no$");	
  			}
  			
		}
		else
		{
			return name;
			
		}
			
  	}
  	
 	public String getId(String name)
 	{
 		String id = cache.getid(name).toString();
		if(id.equals("no$")==true)
		{
			try
	 		{
				rs2 = database.getSelect("Select id from "+tablename+" where lower(name) =lower('"+name+"')");
				if(rs2.next())
				{
					id = (rs2.getString("id"));
					cache.setCache(id, name);
					rs2.close();
	 				return id;
				}
				else
					return("0");
	 		}
	 		catch(Exception e)
	 		{
	 			System.out.println("utility::GetIdName::getId()::Exception -->>"+e.getMessage());
	 			return("0");	
	 		}
		}
		else
		{
			return id;
		}
		
 	}

 	public Hashtable<Object,Object> ConvertPVtoVT(Hashtable<Object,Object> temp)
	{
 		temp = ConvertToLower.convertHashKey(temp);

 		String pv;
		try
		{
			pv = temp.get("pv").toString();
			String pv_id = getId(pv);
	 		if(pv_id.equals("0"))
			{
				temp.put("vt", pv);
				temp.put("pv", "0");
			}
	 		else
	 			temp.put("vt", "no$");
			System.out.println("utility::GetIdName::ConvertPVtoVT()::TempMap : "+temp);
		}
		catch (Exception e) {
			temp.put("pv","0");
			temp.put("vt", "no$");
		}
		return temp;
	}

	public Hashtable<Object, Object> convertMasterId(Hashtable<Object, Object> temp, String tablename,String process)
 	{
		System.out.println("utility::GetIdName::ConverToId()::TempMap : "+temp);
		ResultSet rs = database.getSelect("select fieldname from command where msgtype='"+tablename+"' and from_master='yes' and process='"+process+"'");
		try
		{
			while(rs.next())
			{
				String key1 = rs.getString("fieldname").toLowerCase().toLowerCase();
				if(temp.containsKey(key1))
				{
					String value1 = getId(temp.get(key1).toString().trim());
					temp.put(key1, value1);
				}
			}
			rs.close();
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println("utility::GetIdName::ConverToId()::TempMap : "+temp);
		return temp;
 	}
 	
 	public Hashtable<Object, Object> convertMasterName(Hashtable<Object, Object> temp1, String tablename, String process)
 	{
 		Hashtable<Object, Object> temp = new Hashtable<Object, Object>();
 		temp.putAll(temp1);
 		System.out.println("utility::GetIdName::ConverToId()::TempMap : "+temp);
		ResultSet rs = database.getSelect("select fieldname from command where msgtype='"+tablename+"' and process ='"+process+"' and from_master='yes'");
		try
		{
			while(rs.next())
			{
				String key1 = rs.getString("fieldname");
				if(temp.containsKey(key1))
				{
					String value1 = getItem(temp.get(key1).toString());
					temp.put(key1, value1);
				}
			}
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println("utility::GetIdName::ConverToId()::TempMap : "+temp);
		return temp;
 	}
 	

 	 public Hashtable<Object, Object> ConvertKeyValuetoMasterId(Hashtable<Object, Object>temp,String table_name)
     {
 	 	Hashtable<Object, Object>table = new Hashtable<Object, Object>();
 		Enumeration<Object>tableenumeration = temp.keys();
 		ResultSet rs =  database.getSelect("select fieldname from  command where from_master='yes' and msgtype='"+table_name+"'");
 		Vector<Object> yesfrommaster =new Vector<Object>();
 		try {
			while(rs.next())
			{
 			yesfrommaster.add(rs.getString("fieldname"));
			}
		} 
 		catch (SQLException e) 
		{
			e.printStackTrace();
		}
 		while(tableenumeration.hasMoreElements())
 		{
 			Object key = tableenumeration.nextElement();
 			String keyStr = getId(key.toString());
 			if(keyStr.equals("no$") || keyStr.equals("0"))
 				keyStr = key.toString();
 			if(yesfrommaster.contains(key))
 			{
	 			String value = getId(temp.get(key).toString());
	 			table.put(keyStr, value);
	 			table.remove(key);
 			}
 			else
 				table.put(keyStr,temp.get(key) );
 		}
 		return table;
     }
 	public Hashtable<Object, Object> ConvertKeytoMasterId(Hashtable<Object, Object>temp,String process,String tablename)
 	{
     	Hashtable<Object, Object>table = new Hashtable<Object, Object>();
 		Enumeration<Object>tableenumeration = temp.keys();
 		/*ResultSet rs =  database.getSelect("select fieldname from  command where from_master='yes' and msgtype='"+tablename+"' and process='"+process+"'");
 		Vector<Object> yesfrommaster =new Vector<Object>();
 		try {
			while(rs.next())
			{
 			yesfrommaster.add(rs.getString("fieldname"));
			}
		} 
 		catch (SQLException e) 
		{
			e.printStackTrace();
		}*/
 		while(tableenumeration.hasMoreElements())
 		{
 			Object key = tableenumeration.nextElement();
 				String value = temp.get(key).toString(); 				
	 			String newkey = getId(key.toString());
	 		if(!newkey.equals("0"))	
	 			table.put(newkey, value);
 			else
 				table.put(key,temp.get(key));
 		}
 		return table;
     
 	}
 	 public Hashtable<Object, Object> ConvertKeytoMasterId(Hashtable<Object, Object>temp)
 	 {
 		Hashtable<Object, Object>table = new Hashtable<Object, Object>();
 		Enumeration<Object>tableenumeration = temp.keys();
 		while(tableenumeration.hasMoreElements())
 		{
 			Object key = tableenumeration.nextElement();
 			String value = (temp.get(key).toString());
 			String keyStr =getId(key.toString());
 			if(keyStr.equals("no$") || keyStr.equals("0"))
 				keyStr = key.toString();
 			table.put(keyStr, value);
 		}
 		return table;
 	 }
 	public Hashtable<Object, Object> ConvertKeytoMasterName(Hashtable<Object, Object>temp)
	 {
		Hashtable<Object, Object>table = new Hashtable<Object, Object>();
		Enumeration<Object>tableenumeration = temp.keys();
		while(tableenumeration.hasMoreElements())
		{
			Object key = tableenumeration.nextElement();
			String value = (temp.get(key).toString());
			String keyStr =getItem(key.toString());
			if(keyStr.equals("no$") || key.equals("0"))
				keyStr = key.toString();
			table.put(keyStr, value);
		}
		return table;
	 }
 	public Hashtable<Object, Object> Convertno$ToZero(Hashtable<Object, Object>temp)
	 {
		Hashtable<Object, Object>table = new Hashtable<Object, Object>();
		Enumeration<Object>tableenumeration = temp.keys();
		while(tableenumeration.hasMoreElements())
		{
			Object key = tableenumeration.nextElement();
			String value = (temp.get(key).toString());
			if(value.equals("no$"))
				value = "0";
			table.put(key, value);
		}
		return table;
	 }
 	public static Hashtable<Object,Object> removeno$(Hashtable<Object,Object> temp)
	{
		Enumeration< Object> enum1 = temp.keys();
		while(enum1.hasMoreElements())
		{
			String key = enum1.nextElement().toString();
			String value  = temp.get(key).toString();
			if(value.equals("no$"))
				temp.remove(key);
			else
				temp.put(key.toLowerCase(), value);
		}
		return temp;
	}

 	public Hashtable<Object, Object> getHashMapOfValues(Hashtable<Object, Object> Map)
	{
		Enumeration< Object> enum1 = Map.keys();
		while(enum1.hasMoreElements())
		{
			String key = enum1.nextElement().toString();
			String value  = Map.get(key).toString();
			if(value.equals("no$"))
			{
				Map.remove(key);
			}
			
		}
		return Map;
	}
 	
 	
 	
 	public Hashtable<Object, Object> removeDuplFromhash(Hashtable<Object, Object> FromHash, Hashtable<Object, Object> ConditionHash) 
	{
		try 
		{
			Enumeration< Object> enum1 = FromHash.keys();
			if(enum1.hasMoreElements())
			{
					String key = enum1.nextElement().toString();
					if(ConditionHash.containsKey(key))
					{
						ConditionHash.remove(key);
					}
			}
			return ConditionHash;
		}
		catch (Exception e) 
		{
			return ConditionHash;
		}
		
	}
 	
 	public  Hashtable<Object, Object> ConvertvalueToMasterName(Hashtable<Object, Object> hashobj,String process,String tablename)
	{
		Enumeration< Object> enum1 = hashobj.keys();
		while(enum1.hasMoreElements())
		{
			String key = enum1.nextElement().toString();
			ResultSet rs =  database.getSelect("select from_master from  command where fieldname='"+key+"' and process='"+process+"' and msgtype='"+tablename+"'");
			
			try
			{
				if(rs.next())
				{
					String from_master = rs.getString("from_master");
					if(from_master.equals("yes"))
					{
						String value1 = getItem(hashobj.get(key).toString());
						hashobj.put(key, value1);
					}
					else
					{
						String value1 = hashobj.get(key).toString();
						hashobj.put(key, value1);
						
					}
				}	
				else
				{
					String value1 = hashobj.get(key).toString();
					hashobj.put(key, value1);
					
				}
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return hashobj;
	}
	

 	
	public  Hashtable<Object, Object> CheckFromMaster(Hashtable<Object, Object> hashobj,String process)
	{
		Enumeration< Object> enum1 = hashobj.keys();
		while(enum1.hasMoreElements())
		{
			String key = enum1.nextElement().toString();
			ResultSet rs =  database.getSelect("select from_master from  command where fieldname='"+key+"' and process='"+process+"'");
			
			try
			{
				rs.next();
					String from_master = rs.getString("from_master");
					if(from_master.equals("yes"))
					{
						String value1 = getItem(hashobj.get(key).toString());
						hashobj.put(key, value1);
					}
					else
					{
						String value1 = hashobj.get(key).toString();
						hashobj.put(key, value1);
						
					}
					
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return hashobj;
	}
	
	public Hashtable<Object,Object>getRef(Hashtable<Object,Object> temp)
	{
		try
		{
			temp.get("ref").toString();
		}
		catch(Exception e)
		{
			temp.put("ref",0);
		}
		return temp;
	}
	
	public Hashtable<Object, Object> convertMasterName(Hashtable<Object, Object> temp, String tablename)
 	{
 		System.out.println("utility::GetIdName::ConverToId()::TempMap : "+temp);
		ResultSet rs = database.getSelect("select fieldname from command where msgtype='"+tablename+"' and from_master='yes'");
		try
		{
			while(rs.next())
			{
				String key1 = rs.getString("fieldname");
				if(temp.containsKey(key1))
				{
					String value1 = getItem(temp.get(key1).toString());
					temp.put(key1, value1);
				}
			}
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println("utility::GetIdName::ConverToId()::TempMap : "+temp);
	
		
		return temp;
		
 	}
	public static void main(String[] args) 
	{
		 DBManager db=new DBManager();
 		db.getConnect();
 		String name="Uom";
 		String id="2";
 		String table_name="property_details";
 		String process="add";
 		GetIdName gin=new GetIdName( db);
 		//System.out.println("Output is"+gin.getId(name));
 		//System.out.println("Output is"+gin.getItem(id));
 		Hashtable<Object,Object> temp=new Hashtable<Object, Object>();
 		//temp.put("Pid" ,"10");
 		//temp.put("Pv","14");
 		/*temp.put("Type","2");
 		temp.put("State","4");*/
 		//temp.put("PO","test1");
	//	temp.put("Qty","test2");
 		/*temp.put("pid","71");
		temp.put("pv","72");*/
		temp.put("pid","1");
		temp.put("pv","test2");
		temp.put("vt","spchar");
 		//Hashtable<Object,Object> actaul=gin.ConvertPVtoVT(temp);
 		//Hashtable<Object,Object> actaul=gin.ConvertKeytoMasterId(temp);
 		//Hashtable<Object,Object> actaul=gin.ConvertKeytoMasterName(temp);
 		//Hashtable<Object,Object> actaul=gin.ConvertKeyValuetoMasterId(temp, table_name);
		//Hashtable<Object,Object> actaul=gin.convertMasterId(temp, table_name, process);
 		//Hashtable<Object,Object> actaul=gin.convertMasterName(temp, table_name);
 		//Hashtable<Object,Object> actaul=gin.convertMasterName(temp, table_name, process);
 		//Hashtable<Object,Object> actaul=gin.Convertno$ToZero(temp);
 		//Hashtable<Object,Object> actaul=gin.ConvertKeyValuetoMasterId(temp, table_name);
 		Hashtable<Object,Object> actaul=gin.getRef(temp);
 		System.out.println("Output is"+temp);
// 		System.out.println("Output is"+actual);
	}
}
